package com.luck.dto;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@Slf4j
@RestController
@RequestMapping("/api/sentinel")
@RefreshScope
public class SentinelController1 {
    @GetMapping
    @SentinelResource(value = "order1", blockHandler = "blockHandler",blockHandlerClass = SentinelController1.class)
    public String test() {
        log.debug("下单成功");
        return "下单成功";
    }
    public static String blockHandler(BlockException ex) {
        log.debug("blockHandler");
        return "blockHandler";
    }

//    @PostConstruct
    private void init() {
        ArrayList<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("order1");
        rule.setCount(10);
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);

    }
}
